home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4767 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. Path: qualcomm.com!usenet
  2. From: nabbasi@qualcomm.com (Nasser Abbasi)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Asynchronous I/O, Re: cin.get() function challenge
  5. Date: 1 Feb 1996 01:21:27 GMT
  6. Organization: QUALCOMM
  7. Message-ID: <4ep4in$a31@qualcomm.com>
  8. References: <4eavds$d0u@news.cencom.net> <ALUN.CHAMPION.96Jan29143951@g7240065.bridge.bst.bls.com> <bredelin-3101961313580001@apm-b325-8.ucsd.edu>
  9. NNTP-Posting-Host: nabbasi.qualcomm.com
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.14
  12.  
  13. In article <bredelin-3101961313580001@apm-b325-8.ucsd.edu>, 
  14. bredelin@sdcc13.ucsd.edu says...
  15. >
  16. >I've been trying to find a read routine that doesn't block (i.e. wait to
  17. >return) until a carriage return is entered.  My program needs to
  18. >continuously process data, stopping periodically to process any input 
  19. that
  20. >may have enterred the keyboard buffers since that last time it checked.
  21. >
  22. >
  23. >A. On method would be the check the size of the keyboard buffer, called
  24. >cin.get() only if a complete line is present.  That would be optimal, 
  25. and
  26. >would not involve any blocking.  However, whoever wrote iostream went to
  27. >great lengths to make sure that checking the size of cin's buffer is
  28. >really nasty.  I think I would have to write my own io library just to
  29. >check the size of the keyboard buffer!
  30. >
  31.  
  32. well, Assuming you are on an environment that supports real threads, 
  33. there is a much simpler way to do this than writing an IO library !
  34.  
  35. Create a thread that its job in life is to read complete lines
  36. from the keyboard, let it block as it wants. When the input is
  37. complete, it queues the input to another thread.
  38.  
  39. Now you are out of the realm of the IO stream stuff, and can use
  40. system API calls to do inter-threads interface stuff. which in
  41. system with threads (such as WIN95, NT, Solaris, OS/2, etc..) these
  42. system services are avaliable.
  43.  
  44. What you then have is a client thread that reads input, have a server
  45. thread that loops processing input from its input queue. the client
  46. thread writes lines of data (messages) into the server input queue, and 
  47. signal the server thread to wake , if the server thread has nothing else 
  48. to do, it might be sleep waiting for data to arrive and will wake up, if
  49. it has work to do, next time it checks its input queue, it will find
  50. data there, and will not sleep (no harm of the signal being lost then).
  51.  
  52. This way yout program can do usefull work, and at the same time part
  53. of it is blocked waiting for a complete line to be typed in, which is
  54. what you want to accomplish.
  55.  
  56. So, all what you need is a thread-safe QUEUE class, a semophore signaling 
  57. mechanism (system API), and couple of little threads you create from the 
  58. main(), something like 50-100 lines of C++ code and and you  are all 
  59. set !
  60.  
  61. Nasser
  62.  
  63.